import { request } from '@/utils/request'
import { systemApi } from './common'
import type { 
  AppSettings, 
  SettingsItem, 
  SettingsCategory, 
  SettingsChangeLog,
  SettingsExport,
  SettingsTemplate 
} from '@/types/settings'

// 设置API基础路径
const SETTINGS_BASE_URL = '/api/settings'

// 基础设置API
export const getSettingsApi = (): Promise<AppSettings> => {
  return request.get(SETTINGS_BASE_URL)
}

export const updateSettingsApi = (settings: Partial<AppSettings>): Promise<void> => {
  return request.put(SETTINGS_BASE_URL, settings)
}

export const getSettingsCategoriesApi = (): Promise<SettingsCategory[]> => {
  return request.get(`${SETTINGS_BASE_URL}/categories`)
}

export const getSettingsItemsApi = (category?: string): Promise<SettingsItem[]> => {
  return request.get(`${SETTINGS_BASE_URL}/items`, { params: { category } })
}

export const updateSettingItemApi = (key: string, value: unknown): Promise<void> => {
  return request.put(`${SETTINGS_BASE_URL}/items/${key}`, { value })
}

export const batchUpdateSettingsApi = (items: Array<{ key: string; value: unknown }>): Promise<void> => {
  return request.put(`${SETTINGS_BASE_URL}/items/batch`, { items })
}

export const resetSettingsApi = (category?: string): Promise<void> => {
  return request.post(`${SETTINGS_BASE_URL}/reset`, { category })
}

// 设置变更记录API
export const getSettingsChangeLogApi = (params?: {
  page?: number
  size?: number
  key?: string
  startDate?: string
  endDate?: string
}): Promise<{
  records: SettingsChangeLog[]
  total: number
  page: number
  size: number
}> => {
  return request.get(`${SETTINGS_BASE_URL}/changelog`, { params })
}

// 文件操作API - 使用通用文件工具
export const exportSettingsApi = (category?: string): Promise<SettingsExport> => {
  return request.get(`${SETTINGS_BASE_URL}/export`, { category })
}

export const importSettingsApi = (file: File): Promise<void> => {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('folder', 'settings')
  
  return request.post(`${SETTINGS_BASE_URL}/import`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 设置模板API
export const getSettingsTemplatesApi = (): Promise<SettingsTemplate[]> => {
  return request.get(`${SETTINGS_BASE_URL}/templates`)
}

export const createSettingsTemplateApi = (template: Omit<SettingsTemplate, 'id' | 'createdAt' | 'updatedAt'>): Promise<SettingsTemplate> => {
  return request.post(`${SETTINGS_BASE_URL}/templates`, template)
}

export const applySettingsTemplateApi = (templateId: string): Promise<void> => {
  return request.post(`${SETTINGS_BASE_URL}/templates/${templateId}/apply`)
}

export const deleteSettingsTemplateApi = (templateId: string): Promise<void> => {
  return request.delete(`${SETTINGS_BASE_URL}/templates/${templateId}`)
}

// 设置验证API
export const validateSettingsApi = (settings: Partial<AppSettings>): Promise<{
  valid: boolean
  errors: Array<{ key: string; message: string }>
}> => {
  return request.post(`${SETTINGS_BASE_URL}/validate`, settings)
}

// 系统信息API - 使用通用系统工具
export const getSystemInfoApi = () => {
  return systemApi.getInfo()
}

// 设置测试API
export const testSettingsApi = (settings: Partial<AppSettings>): Promise<{
  success: boolean
  message: string
  details?: unknown
}> => {
  return request.post(`${SETTINGS_BASE_URL}/test`, settings)
}

// 设置统计API
export const getSettingsStatsApi = (): Promise<{
  totalSettings: number
  modifiedSettings: number
  lastModified: string
  categoriesCount: number
  templatesCount: number
}> => {
  return request.get(`${SETTINGS_BASE_URL}/stats`)
}
